package com.yuejian.neusoft.yuejian_24.fragment;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.oragee.banners.BannerView;
import com.squareup.picasso.Picasso;
import com.yuejian.neusoft.yuejian_24.R;
import com.yuejian.neusoft.yuejian_24.activity.SearchActivity;
import com.yuejian.neusoft.yuejian_24.adapter.ActivityListAdapter;
import com.yuejian.neusoft.yuejian_24.adapter.MarchListAdapter;
import com.yuejian.neusoft.yuejian_24.adapter.MasterListAdapter;
import com.yuejian.neusoft.yuejian_24.javaBean.Activity;
import com.yuejian.neusoft.yuejian_24.javaBean.March;
import com.yuejian.neusoft.yuejian_24.javaBean.Master;
import com.yuejian.neusoft.yuejian_24.listener.ListListener;
import com.yuejian.neusoft.yuejian_24.model.ActivityModel;
import com.yuejian.neusoft.yuejian_24.model.MarchModel;
import com.yuejian.neusoft.yuejian_24.model.MasterModel;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements View.OnClickListener{
    private String[] imgs = {"1.jpg","2.jpg","3.jpg","4.jpg"};
    private List<View> viewList;
    BannerView bannerView;
    private TextView activity, master, game;
    private View view = null;
    private RecyclerView recyclerview;
    private ImageView search;

    ListListener<Activity> Aclistener = new ListListener<Activity>() {
        @Override
        public void onResponse(List<Activity> t) {
            recyclerview.setAdapter(new ActivityListAdapter(getContext(), t));
        }

        @Override
        public void onFail() {

        }
    };

    ListListener<Master> Mlistener = new ListListener<Master>() {
        @Override
        public void onResponse(List<Master> t) {
            recyclerview.setAdapter(new MasterListAdapter(getActivity(), t));
        }

        @Override
        public void onFail() {

        }
    };

    ListListener<March> Glistener = new ListListener<March>() {
        @Override
        public void onResponse(List<March> t) {
            recyclerview.setAdapter(new MarchListAdapter(getActivity(), t));
        }

        @Override
        public void onFail() {

        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, container, false);
        initRecyclerview(view);
        ActivityModel activityModel = new ActivityModel();
        activityModel.getactivitylist(Aclistener);
        initViews();
        initEvents();
        viewList = new ArrayList<View>();
        for (int i = 0; i < imgs.length; i++) {
            ImageView image = new ImageView(getActivity());
            image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //设置显示格式
            image.setScaleType(ImageView.ScaleType.FIT_XY);
            Picasso.with(getContext()).load("http://10.0.2.2/yuejian/Public/photo/"+imgs[i]).into(image);
            viewList.add(image);
        }
        bannerView.startLoop(true);
        bannerView.setViewList(viewList);
        return view;
    }

    private void initEvents() {
        activity.setOnClickListener(this);
        master.setOnClickListener(this);
        game.setOnClickListener(this);
        search.setOnClickListener(this);
    }

    private void initViews() {
        activity = (TextView) view.findViewById(R.id.home_activity);
        master = (TextView) view.findViewById(R.id.home_master);
        game = (TextView) view.findViewById(R.id.home_march);
        bannerView = (BannerView)view.findViewById(R.id.banner);
        search = (ImageView)view.findViewById(R.id.home_search);
    }

    private void initRecyclerview(View view) {
        recyclerview = (RecyclerView) view.findViewById(R.id.rv_home);
        recyclerview.setLayoutManager(new LinearLayoutManager(this.getActivity()));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home_activity:
                activity.setTextColor(Color.parseColor("#000000"));
                master.setTextColor(Color.parseColor("#C8C8C8"));
                game.setTextColor(Color.parseColor("#C8C8C8"));
                ActivityModel activityModel = new ActivityModel();
                activityModel.getactivitylist(Aclistener);
                break;

            case R.id.home_master:
                activity.setTextColor(Color.parseColor("#C8C8C8"));
                master.setTextColor(Color.parseColor("#000000"));
                game.setTextColor(Color.parseColor("#C8C8C8"));
                MasterModel masterModel = new MasterModel();
                masterModel.masterlist(Mlistener);
                break;

            case R.id.home_march:
                activity.setTextColor(Color.parseColor("#C8C8C8"));
                master.setTextColor(Color.parseColor("#C8C8C8"));
                game.setTextColor(Color.parseColor("#000000"));
                MarchModel gameModel = new MarchModel();
                gameModel.getmarchlist(Glistener);
                break;
            case R.id.home_search:
            Intent intent = new Intent(getActivity(), SearchActivity.class);
            startActivity(intent);
            break;
        }
    }
}
